<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: NewsletterEdit.php 461 2011-01-16 18:04:56Z steen.rabol $
 *
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Portlets.THSpillerProfilPortlet.Common.THSpillerProfilPortletRecord');
Prado::using('Application.Common.Data.PortalUserRecord');

class NewsletterEdit extends PortalAdminPage
{
	private $_pRecord	= null;

	public function onInit($p)
	{
		parent::onInit($p);
		if(isset($this->Request['id']) && TPropertyValue::ensureInteger($this->Request['id']) != 0)
		{
			$this->ProfileRecord	= THSpillerProfilPortletRecord::finder()->findByid(TPropertyValue::ensureInteger($this->Request['id']));
		}
		else
		{
			$this->ProfileRecord	= new THSpillerProfilPortletRecord();
		}
	}

	public function getProfileRecord()
	{
		return $this->_pRecord;
	}

	public function setProfileRecord($value)
	{
		$this->_pRecord	= $value;
	}
	public function  onLoad($param)
	{
		parent::onLoad($param);
		PortalUtil::Info($this->ispostback);
		if(!$this->IsPostBack)
        {
			$this->dlUser->DataSource	= PortalUserRecord::finder()->findAll();

			if(!is_null($this->ProfileRecord->imagefilename) && $this->ProfileRecord->imagefilename != '')
			{
				$this->ProfilImage->Enabled = true;
				$this->ProfilImage->Visible = true;
				$this->ProfilImage->ImageUrl	= $this->Service->Request->BaseUrl . '/storage/THSpillerProfiler/' . $this->ProfileRecord->imagefilename;
			}

			$this->dlUser->SelectedValue = $this->ProfileRecord->userid;
			$this->dlUser->dataBind();
		}

		PortalUtil::Info($this->Firstname->Text);
	}

	public function SaveSpillerProfil($sender, $param)
	{

		if($this->IsValid)
		{
			if(is_null($this->ProfileRecord))
			{
				$this->ProfileRecord	= new THSpillerProfilPortletRecord();
			}

			$this->ProfileRecord->firstname		= $this->Firstname->Text;
			$this->ProfileRecord->lastname		= $this->Lastname->Text;
			$this->ProfileRecord->imagefilename	= $this->ImageFilename->Value;
			$this->ProfileRecord->userid		= $this->dlUser->SelectedValue;
			$this->ProfileRecord->description	= $this->Description->Text;
			$this->ProfileRecord->phone			= $this->Phone->Text;
			$this->ProfileRecord->address		= $this->Address->Text;
			$this->ProfileRecord->zip			= $this->Zip->Text;
			$this->ProfileRecord->city			= $this->City->Text;
			$this->ProfileRecord->country		= $this->Country->Text;

			$this->ProfileRecord->save();
			//PortalUtil::Info($this->ProfileRecord);
			$this->gotoPage('Admin.Portlets.THSpillerProfilPortlet.ProfileList');
		}
	}

	public function fileUploaded($sender, $param)
	{
		if($sender->HasFile)
		{
			$savepath	= str_replace("\\", "/",dirname($this->getRequest()->getApplicationFilePath()));
			$savepath 	.= '/';
			$savepath	.= 'storage/THSpillerProfiler/';
			$savepath	.= $sender->FileName;

			$sender->saveAs($savepath);
			$imgattr	= getimagesize($savepath);

			$this->ProfilImage->Enabled		= true;
			$this->ProfilImage->Visible		= true;
			$this->ProfilImage->ImageUrl	= $this->Service->Request->BaseUrl . '/storage/THSpillerProfiler/' . $sender->FileName;
			$this->ImageFilename->Value		= $sender->FileName;

			if(is_null($this->ProfileRecord))
			{
				$this->ProfileRecord	= new THSpillerProfilPortletRecord();
			}

			$this->ProfileRecord->firstname		= $this->Firstname->Text;
			$this->ProfileRecord->lastname		= $this->Lastname->Text;
			$this->ProfileRecord->imagefilename	= $this->ImageFilename->Value;
			$this->ProfileRecord->userid		= $this->dlUser->SelectedValue;
			$this->ProfileRecord->description	= $this->Description->Text;
			$this->ProfileRecord->phone			= $this->Phone->Text;
			$this->ProfileRecord->address		= $this->Address->Text;
			$this->ProfileRecord->zip			= $this->Zip->Text;
			$this->ProfileRecord->city			= $this->City->Text;
			$this->ProfileRecord->country		= $this->Country->Text;

			//$this->ProfileRecord->save();


/*
			$imagefilespath	= $this->Application->PortalBasePath . '/storage/THSpillerProfiler';
			$imagefiles = PortalUtil::getFilesInPath($imagefilespath,"name");
			$this->ImageName->DataSource	= $imagefiles;
			$this->ImageName->dataBind();

			if(is_array($imagefiles))
			{
				foreach($imagefiles as $im)
				{
					 if(in_array($sender->FileName,$im))
						$this->ImageName->SelectedValue = $sender->FileName;
				}
			}

			$this->ImageWidth->Text		= $imgattr[0];
			$this->ImageHeight->Text	= $imgattr[1];
			*/
		}

	}
}
?>